// pages/onlineclass/onlineclass.js
import { Onlineclass } from '../../model/onlineclass.js'
const onlineclass = new Onlineclass();
import { config } from '../../config.js'
var WxParse = require("../../wxParse/wxParse.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentHeight:'',
    page:0,
    classList:[],
    hidden:true,
    content:'',
    // id:''
    lesttime:0,
    loging:'没有更多了',
    count:0,
    isShow:0,
    content:''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  // 关于我们
  about_us_content(){
    var that=this
    onlineclass.about_us_content().then(res=>{
      console.log(res)
      var article=res.data.aboutUs.content;
      console.log(article);
      WxParse.wxParse('article', 'html', article,that,1)
    }).catch(error=>{
      console.log(error)
    })
  },
  // 查看隐藏或展示
  getShowOrHide(){
    var that=this
    onlineclass.getShowOrHide().then(res=>{
      console.log(res)
      that.setData({
        isShow:res.data.isShow
      })
      if(res.data.isShow==0){
        that.about_us_content()
      }else{
        that.setData({
          classList:[],
          page:0
        })
        that.class_list()
        that.homePageHeight();
      }
    }).catch(error=>{
      console.log(error)
    })
  },

  //下拉刷新的修改
  homePageHeight(){
    var that = this;
    wx.getSystemInfo({
      complete: (res) => {
        that.setData({
          currentHeight:Number(res.windowHeight) + Number(85)
        })
      },
    })
  },
  //首页高度
  homePageHeight(){
    var that = this;
    var scrollHeight=wx.getStorageSync('height')
    wx.getSystemInfo({
      complete: (res) => {
        console.log(res)
        that.setData({
          currentHeight:res.windowHeight-scrollHeight
        })
      },
    })
  },
  //点击视频进入详情
  classDetails(e){
    console.log(e)
    var curTime = e.timeStamp
    var lastTime = this.data.lesttime
    
			if (curTime - lastTime > 0) {
				if (curTime - lastTime < 1000) {//是双击事件
          console.log("快了")
          
          this.setData({
            lesttime:curTime,
          })
        }
        else{




          this.setData({
            content:e.currentTarget.dataset.item.content,
            lesttime:curTime,
          })
          wx.navigateTo({
            url: '/pages/classdetails/classdetails?id=' + e.currentTarget.dataset.item.id+'&videoId='+e.currentTarget.dataset.item.onlineVideoId,
          })
          wx.setStorage({
            data: this.data.content,
            key: 'classContent',
            success(res){
              // wx.navigateTo({
              //   url: '/pages/classdetails/classdetails?id=' + e.currentTarget.dataset.item.onlineVideoId,
              // })
            },
            fail(error){
      
            }
          })




        }
			}
    
  },
  
  class_list(){
    var obj={page:this.data.page}
    onlineclass.online_class_list(obj).then(res=>{
      // console.log(res.data.datums)
      var classList=this.data.classList
      
        for(var i=0;i<res.data.datums.length;i++){
          
            if(res.data.datums[i].isHot==1){
              
              res.data.datums[i].hidden=true
            }else if(res.data.datums[i].isHot==0){
              
              res.data.datums[i].hidden=false
            }
            classList.push(res.data.datums[i])
            // console.log(res.data.datums[i].content)
            
            // console.log(res.data.datums[i].id)
        }

      this.setData({
        classList:classList,
        count:res.data.count
        
      })
    }).catch(error=>{
      console.log(error)
    })
  },
  //下拉刷新
  onPulling(e) {
    // console.log('onPulling:', e)
    },
    
    onRefresh() {
    if (this._freshing) return
    this._freshing = true
    this.setData({
      classList:[],
      page:0,
    })
    this.homePageHeight();
    this.class_list()
      this.setData({
        triggered: false,
        })
        this._freshing = false
    },
    
    onRestore(e) {
    // console.log('onRestore:', e)
    },
    
    onAbort(e) {
    // console.log('onAbort', e)
    },
    bindscrolltolower(e){
      console.log("底部")
      console.log(e)
      if (Number(this.data.count)> Number(this.data.classList.length)){
        console.log('分页');
        this.setData({
          page: this.data.page + 1,
          loging:'加载中...'
        });
        this.my_activity()
      }else{
        console.log("else")
        this.setData({
          loging:'没有更多了'
        })
      }
    },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getShowOrHide()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // if(this.data.count>this.data.classList.length){
    //   this.setData({
    //       page:this.data.page+1,
    //       loging:'加载中...'
    //   })
    //   this.my_activity()
    // }else{
    //     this.setData({
    //         loging:'没有更多了'
    //     })
    // }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})